<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/Welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类参数</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <!-- 头部警示提示消息 -->
      <el-alert
        title="注意：只允许为第三级分类设置相关参数！"
        type="warning"
        show-icon
        :closable="false"
      >
      </el-alert>

      <!-- 选择商品分类区域 -->
      <el-row class="cat_opt">
        <el-col>
          <span>选择商品分类：</span>

          <!-- 选择商品分类的级联选择框 -->
          <el-cascader
            v-model="selectedCateKey"
            :options="cateList"
            :props="PropInfo"
            @change="selectedCateChange"
          ></el-cascader>
        </el-col>
      </el-row>

      <!-- 参数标签区域 -->
      <el-tabs v-model="activeName" @tab-click="tabClicked">
        <!-- 动态参数区域 -->
        <el-tab-pane label="动态参数" name="many">
          <!-- 添加参数按钮 -->
          <el-button
            type="primary"
            :disabled="isTabDisabled"
            @click="showAddParamDialog"
            >添加参数</el-button
          >

          <!-- table 区域 -->
          <el-table :data="manyList" stripe style="width: 100%" border>
            <!-- 展开行区域 -->
            <el-table-column type="expand">
              <template slot-scope="scope">
                <el-tag
                  v-for="(item, i) in scope.row.attr_vals"
                  :key="i"
                  closable
                >
                  {{ item }}
                </el-tag>
                <!-- 添加一个新的tag区域 -->
                <!-- <el-input
                  class="input-new-tag"
                  v-if="inputVisible"
                  v-model="inputValue"
                  ref="saveTagInput"
                  size="small"
                  @keyup.enter.native="handleInputConfirm"
                  @blur="handleInputConfirm"
                >
                </el-input>
                <el-button
                  v-else
                  class="button-new-tag"
                  size="small"
                  @click="showInput"
                  >+ New Tag</el-button
                > -->
              </template>
            </el-table-column>
            <el-table-column type="index" label="#"></el-table-column>
            <el-table-column prop="attr_name" label="参数名称">
            </el-table-column>
            <!-- 操作 -->
            <el-table-column prop="name" label="操作">
              <el-button type="primary" icon="el-icon-edit" size="mini"
                >编辑</el-button
              >
              <el-button type="danger" icon="el-icon-delete" size="mini"
                >删除</el-button
              >
            </el-table-column>
          </el-table>
        </el-tab-pane>

        <!-- 静态参数区域 -->
        <el-tab-pane label="静态参数" name="only">
          <!-- 添加属性按钮 -->
          <el-button
            type="primary"
            :disabled="isTabDisabled"
            @click="showAddParamDialog"
            >添加属性</el-button
          >
          <!-- table 区域 -->
          <el-table :data="onlyList" stripe style="width: 100%" border>
            <el-table-column type="expand"></el-table-column>
            <el-table-column type="index" label="#"></el-table-column>
            <el-table-column prop="attr_name" label="参数名称">
            </el-table-column>
            <!-- 操作 -->
            <el-table-column prop="name" label="操作">
              <el-button type="primary" icon="el-icon-edit" size="mini"
                >编辑</el-button
              >
              <el-button type="danger" icon="el-icon-delete" size="mini"
                >删除</el-button
              >
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-card>

    <!-- 添加参数对话框 -->
    <el-dialog
      :title="'添加' + titleText"
      :visible.sync="addParamDialogVisible"
      width="50%"
      @close="addDialogclosed"
    >
      <!-- 表单区域 -->
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="100px"
      >
        <el-form-item :label="titleText" prop="attr_name">
          <el-input v-model="addForm.attr_name"></el-input>
        </el-form-item>
      </el-form>

      <!-- 底部按钮 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addParamDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addParamDialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 商品分类列表
      cateList: [],
      //选中的商品分类
      selectedCateKey: [],
      //prop 属性信息
      PropInfo: {
        expandTrigger: "hover",
        value: "cat_id",
        label: "cat_name",
        children: "children",
      },
      // 被激活的页标签名称
      activeName: "many",
      // 动态参数列表
      manyList: [],
      //静态参数列表
      onlyList: [],
      //控制添加参数对话框的显示与隐藏
      addParamDialogVisible: false,
      //添加参数的表单
      addForm: {
        attr_name: "",
      },
      //添加参数的表单验证规则
      addFormRules: {
        attr_name: [
          {
            required: true,
            message: "请输入参数名称！",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getCateList();
  },
  methods: {
    //获取商品分类列表
    async getCateList() {
      const { data: res } = await this.$http.get("categories", {
        params: { type: 3 },
      });
      if (res.meta.status !== 200) {
        return this.$message.error("获取商品分类列表失败！");
      }
      //console.log(res);
      this.cateList = res.data;
    },

    //当选中节点变化时触发
    selectedCateChange() {
      //只能选择三级分类
      if (this.selectedCateKey.length !== 3) {
        this.selectedCateKey = [];
        return;
      }
      this.getParameList();

      console.log(this.selectedCateKey);
    },

    //点击切换tab分页选项时调用
    tabClicked() {
      this.getParameList();
    },

    // 获取参数列表
    async getParameList() {
      const { data: res } = await this.$http.get(
        `categories/${this.cateId}/attributes`,
        {
          params: { sel: this.activeName },
        }
      );
      if (res.meta.status !== 200) {
        return this.$message.error("获取参数列表失败！");
      }
      //console.log(res.data);
      // 将字符串拆分为数组
      res.data.forEach((item) => {
        item.attr_vals = item.attr_vals.split(" ");
      });
      if (this.activeName === "many") {
        this.manyList = res.data;
      } else {
        this.onlyList = res.data;
      }
    },

    //展示添加参数对话框
    showAddParamDialog() {
      this.addParamDialogVisible = true;
    },

    //添加参数对话框关闭
    addDialogclosed() {
      this.$refs.addFormRef.resetFields();
    },

    //
  },
  computed: {
    // 当计算结果发生变化时

    //是否禁用属性
    isTabDisabled() {
      if (this.selectedCateKey.length === 3) {
        return false;
      }
      return true;
    },

    //请求参数时的ID
    cateId() {
      if (this.selectedCateKey.length === 3) {
        return this.selectedCateKey[2];
      }
      return null;
    },

    // 添加参数对话框的标题
    titleText() {
      if (this.activeName === "many") {
        return "动态参数";
      } else {
        return "静态属性";
      }
    },
  },
};
</script>

<style scoped>
.cat_opt {
  margin: 15px 0;
}
.el-tag{
  margin: 5px;
}
</style>